package com.grgbanking.it.system.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.grgbanking.it.common.json.JSONHelper;
import com.grgbanking.it.common.util.Converter;
import com.grgbanking.it.common.util.HibernateUtil;
import com.grgbanking.it.system.dao.IResourceDao;
import com.grgbanking.it.system.entity.Resource;
import com.grgbanking.it.system.service.IResourceService;

/**
 * 资源管理
 * 
 * ResourceService
 * 
 * @author 潘广伟
 * @Email p_3er@qq.com
 * @Date 2014-12-13 上午9:55:55
 * 
 * @version 1.0.0
 *
 */
@Service
public class ResourceService implements IResourceService {
	@Autowired
	private IResourceDao<Resource, Integer> resourceDao;

	/**
	 * 获取单个资源
	 */
	@Override
	public String findByIdToJson(Integer id) throws Exception {
		Resource resource = (Resource) HibernateUtil.loadLazyData(resourceDao.findById(id), Resource.class);
		return JSONHelper.convertObjectForForm(resource);
	}

	/**
	 * 保存或更新
	 */
	@Override
	public void saveOrUpdate(Resource resource) throws Exception {
		resourceDao.saveOrUpdate(resource);
	}

	/**
	 * 获取资源树
	 * findToTree
	 * 
	 * @param keyword 关键字
	 * @param roleId 角色ID
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public String findToTree(String keyword,Integer roleId) throws Exception{
		if(roleId == null){
			roleId = 0;
		}
		
		StringBuffer sql = createSQLForTree(roleId);
		
		if(StringUtils.isEmpty(keyword)){
			sql.append(" and (r.parent is null or r.parent=0)");
		}else{
			sql.append(" and r.name like '%").append(keyword).append("%'");
		}
		List<Map> list = resourceDao.listBySQL(sql.toString());
		List<Resource> resources = new ArrayList<Resource>();
		for(Map map : list){
			Resource resource = new Resource();
			resource = (Resource) Converter.convertMapToObject(resource, map,"checked");
			List<Resource> children = getChildren(resource.getId(),roleId);
			if(children.size() > 0){
				resource.setChildren(children);
				resource.setIconCls("parent-node");
			}else{
				resource.setLeaf(true);
				resource.setIconCls("child-node");
			}
			resources.add(resource);
		}
		
		return  JSONHelper.converList(resources);
	}
	
	/**
	 * 资源树SQL
	 * createSQLForTree
	 * 
	 * @param roleId
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public StringBuffer createSQLForTree(Integer roleId){
		StringBuffer sql = new StringBuffer("select r.*,case when m.id is null then 0 else 1 end checked \n");
		sql.append("from resource r left join m_role_resource m\n");
		sql.append("on r.id=m.resource_id and m.role_id=").append(roleId);
		sql.append(" where r.state<>0");
		
		return sql;
	}
	
	/**
	 * 资源树子节点
	 * getChildren
	 * 
	 * @param parentId
	 * @param roleId
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@SuppressWarnings("rawtypes")
	public List<Resource> getChildren(Integer parentId,Integer roleId) throws Exception{
		StringBuffer sql = createSQLForTree(roleId);
		sql.append(" and r.parent=").append(parentId);
		List<Resource> resources = new ArrayList<Resource>();
		List<Map> list = resourceDao.listBySQL(sql.toString());
		
		for(Map map : list){
			Resource resource = new Resource();
			resource = (Resource) Converter.convertMapToObject(resource, map,"checked");
			List<Resource> children = getChildren(resource.getId(),roleId);
			if(children.size() > 0){
				resource.setChildren(children);
				resource.setIconCls("parent-node");
			}else{
				resource.setLeaf(true);
				
				resource.setIconCls("child-node");
			}
			
			resources.add(resource);
		}
		
		return resources;
	}

	/**
	 * 删除
	 */
	@Override
	public void delete(Integer id) throws Exception {
		resourceDao.virtualDelete(id);
	}
	
	
	
}